import unittest
import jieba
import jieba.posseg
import jieba.analyse


class TestAnalyseFiction(unittest.TestCase):

    """
    功能: 从指定的小说中提取出出现次数最多的词
    """
    def test_1(self):
        txt = open("E:\\qq-file\\991264921\\FileRecv\\MobileFile\\jianlai.txt", "r", encoding='utf-8').read()
        words = jieba.lcut(txt)  # 使用精确模式对文本进行分词
        counts = {}  # 通过键值对的形式存储词语及其出现的次数

        for word in words:
            if len(word) == 1:  # 单个词语不计算在内
                continue
            else:
                counts[word] = counts.get(word, 0) + 1  # 遍历所有词语，每出现一次其对应的值加 1

        items = list(counts.items())  # 将键值对转换成列表
        items.sort(key=lambda x: x[1], reverse=True)  # 根据词语出现的次数进行从大到小排序

        for i in range(100):
            word, count = items[i]
            print("{0:<5}{1:>5}".format(word, count))


if __name__ == '__main__':
    unittest.main()
